#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QStringList>
#include <QDebug>
#include <QMessageBox>
#include <QDateTime>
#include <QFileDialog>
#include <cstring>
#include <QStringList>

#define UART_TX_LEN 64

//int pb_open_com_flag;  //串口标志位

char send_buf[UART_TX_LEN];

char *clear_uart_string(void){

    memset(&send_buf,0,UART_TX_LEN);    //清空缓冲区

    return send_buf;

}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_openButton_clicked()
{
    mycom = new QSerialPort();  //创建串口
    mycom->setPortName("ttyUSB0");//串口名称
    mycom->open(QIODevice::ReadWrite);
    mycom->setBaudRate(115200);//串口波特率设置 (115200)
    mycom->setDataBits(QSerialPort::Data8);//设置数据位数（8位)
    mycom->setParity(QSerialPort::NoParity);//设置奇偶校验 (无校验位）
    mycom->setStopBits(QSerialPort::OneStop);//设置停止位（1位)
    mycom->setFlowControl(QSerialPort::NoFlowControl);//设置流控制

    receivethread=new ReceiveThread(mycom);
    this->connect(receivethread,SIGNAL(comRecive()),this,SLOT(RxData()));

    receivethread->start();
}

void MainWindow::RxData()
{
    int usRxLength=0;//缓存数据长度
    QString recbuf;
    RequestData.clear();
    RequestData=mycom->readAll();
    usRxLength=RequestData.size();
    char buf[usRxLength],*updata;
    //ui->revInfo->append(RequestData.data());
    for(int i=0;i<RequestData.count();i++)
    {
        QString s;
        s.sprintf("0x%02x, ",(unsigned char)RequestData.at(i));
        buf[i]=(unsigned char)RequestData.at(i);
        recbuf +=s;
    }
    ui->revInfo->append(recbuf);
    ui->lineEdit_2->setText(QString::number(usRxLength));
    RequestData.clear();
    recbuf.clear();
    while(usRxLength>=11)
    {
        if(!((buf[0]==0x55)&((buf[1]&0x50)==0x50)))
        {
           for (int i = 1; i < usRxLength; i++) buf[i - 1] = buf[i];
           usRxLength--;
           continue;
        }
        if (((buf[0]+buf[1]+buf[2]+buf[3]+buf[4]+buf[5]+buf[6]+buf[7]+buf[8]+buf[9])&0xff)==buf[10])
        {
/*            for(int i=0;i<11;i++)
            {
                updata[i]=buf[i];
            }*/
            updata=buf;
            display(updata);//刷新显示
        }
        for (int i = 11; i < usRxLength; i++) buf[i - 11] = buf[i];
        usRxLength -= 11;
    }
}

void MainWindow::display(char *updata)
{
    char dsbuf[11];
    for(int i=0;i<11;i++)
    {
        dsbuf[i]=*updata;
        updata++;
    }
    switch (dsbuf[1])
    {
        case 0x50:

            break;
        case 0x51:
            //0x55 0x51 加速度
            a[0]=(((dsbuf[3]<<8)&0xff00)|(dsbuf[2]&0x00ff))/32768.0*16;//g,9.8m/s^2
            a[1]=(((dsbuf[5]<<8)&0xff00)|(dsbuf[4]&0x00ff))/32768.0*16;//g,9.8m/s^2
            a[2]=(((dsbuf[7]<<8)&0xff00)|(dsbuf[6]&0x00ff))/32768.0*16;//g,9.8m/s^2
            a[3]=(((dsbuf[9]<<8)&0xff00)|(dsbuf[8]&0x00ff));//  ℃
            for(int t=0;t<3;t++)
            {
                if(a[t]>=16)
                    {
                        a[t]=a[t]-32;
                    }
                else
                    {
                        a[t]=a[t]+0;
                    }
            }
            //Temperature=a[3]/100.0; //0x55 0x51 0x52 0x53 0x54 温度℃
            ui->lineEdit_3->setText(QString::number(a[0]));//x轴加速度
            ui->lineEdit_4->setText(QString::number(a[1]));//y轴加速度
            ui->lineEdit_5->setText(QString::number(a[2]));//z轴加速度
            break;
        case 0x52:
            //0x55 0x52 角速度
            w[0]=(((dsbuf[3]<<8)&0xff00)|(dsbuf[2]&0x00ff))/32768.0*2000;
            w[1]=(((dsbuf[5]<<8)&0xff00)|(dsbuf[4]&0x00ff))/32768.0*2000;
            w[2]=(((dsbuf[7]<<8)&0xff00)|(dsbuf[6]&0x00ff))/32768.0*2000;
            w[3]=(((dsbuf[9]<<8)&0xff00)|(dsbuf[8]&0x00ff));
            for(int t=0;t<3;t++)
            {
                if(w[t]>=2000)
                {
                   w[t]=w[t]-4000;
                }
                else
                {
                   w[t]=w[t]+0;
                }
            }
            Temperature=w[3]/100.0; //0x55 0x51 0x52 0x53 0x54 温度℃
            ui->lineEdit_6->setText(QString::number(w[0]));//x轴角速度
            ui->lineEdit_7->setText(QString::number(w[1]));//y轴角速度
            ui->lineEdit_8->setText(QString::number(w[2]));//z轴角速度
            ui->lineEdit_15->setText(QString::number(Temperature));//温度
            break;
        case 0x53:
            //0x55 0x53 角度
            Angle[0]=(((dsbuf[3]<<8)&0xff00)|(dsbuf[2]&0x00ff))/32768.0*180;
            Angle[1]=(((dsbuf[5]<<8)&0xff00)|(dsbuf[4]&0x00ff))/32768.0*180;
            Angle[2]=(((dsbuf[7]<<8)&0xff00)|(dsbuf[6]&0x00ff))/32768.0*180;
            Angle[3]=(((dsbuf[9]<<8)&0xff00)|(dsbuf[8]&0x00ff));
            for(int t=0;t<3;t++)
            {
                if(Angle[t]>=180)
                {
                    Angle[t]=Angle[t]-360;
                }
                else
                {
                    Angle[t]=Angle[t]+0;
                }
            }
            //Temperature=Angle[3]/100.0; //0x55 0x51 0x52 0x53 0x54 温度℃
            ui->lineEdit_9->setText(QString::number(Angle[0]));//x轴角度
            ui->lineEdit_10->setText(QString::number(Angle[1]));//y轴角度
            ui->lineEdit_11->setText(QString::number(Angle[2]));//z轴角度
            break;
        case 0x54:
            //0x55 0x54 磁场
            h[0]=(((dsbuf[3]<<8)&0xff00)|(dsbuf[2]&0x00ff));
            h[1]=(((dsbuf[5]<<8)&0xff00)|(dsbuf[4]&0x00ff));
            h[2]=(((dsbuf[7]<<8)&0xff00)|(dsbuf[6]&0x00ff));
            h[3]=(((dsbuf[9]<<8)&0xff00)|(dsbuf[8]&0x00ff));
            for(int t=0;t<3;t++)
            {
                if(h[t]>=32768)
                {
                    h[t]=h[t]-32768;
                }
                else
                {
                    h[t]=h[t]+0;
                }
            }
            //Temperature=h[3]/100.0; //0x55 0x51 0x52 0x53 0x54 温度℃
            ui->lineEdit_12->setText(QString::number(h[0]));//x轴磁场
            ui->lineEdit_13->setText(QString::number(h[1]));//y轴磁场
            ui->lineEdit_14->setText(QString::number(h[2]));//z轴磁场
            break;
        case 0x55:
            //0x55 0x55  端口
            Port[0]=(((dsbuf[3]<<8)&0xff00)|(dsbuf[2]&0x00ff));
            Port[1]=(((dsbuf[5]<<8)&0xff00)|(dsbuf[4]&0x00ff));
            Port[2]=(((dsbuf[7]<<8)&0xff00)|(dsbuf[6]&0x00ff));
            Port[3]=(((dsbuf[9]<<8)&0xff00)|(dsbuf[8]&0x00ff));
            break;
        case 0x56:
            //0x55 0x56  气压  高度
            Pressure[0]=(((dsbuf[5]<<24)&0xff000000)|((dsbuf[4]<<16)&0x00ff0000)|((dsbuf[3]<<8)&0x0000ff00)|(dsbuf[2]&0x000000ff));
            Altitude[0]=(((dsbuf[9]<<24)&0xff000000)|((dsbuf[8]<<16)&0x00ff0000)|((dsbuf[7]<<8)&0x0000ff00)|(dsbuf[6]&0x000000ff));
            if(Pressure[0]>=2147483648)
                Pressure[1]=Pressure[0]-4294967296;//0xffffffff+1
            else
                Pressure[1]=Pressure[0];
            if(Altitude[0]>=2147483648)
                Altitude[1]=(Altitude[0]-4294967296);//0xffffffff+1
            else
                Altitude[1]=Altitude[0];
            ui->lineEdit_16->setText(QString::number(Pressure[1]));//textChanged 气压
            ui->lineEdit_17->setText(QString::number(Altitude[1]));//高度
            break;
        case 0x57:
            //经纬度
            break;
        case 0x58:
            //地速
            break;
        case 0x59:
            //四元素
            break;
        case 0x5A:
            //卫星定位精度
            break;
        default:
            break;
        }
        QDateTime time =QDateTime::currentDateTime();//获得系统现在的时间
        //QString str1=time.toString("yyyy-MM-dd hh:mm:ss ddd");//设置显示格式
        QString str1=time.toString("hh:mm:ss");//设置显示格式
        ui->lineEdit->setText(str1);
}
void MainWindow::on_closeButton_clicked()
{
    mycom->close();
    //delete mycom;
    //mycom = NULL;
    //this->close();
}
